
import gradio as gr
from PIL import Image


# SDK引入模型
from dwspark.config import Config
from dwspark.models import PictureUnderstanding
# 日志
# 加载系统环境变量：SPARKAI_UID、SPARKAI_APP_ID、SPARKAI_API_KEY、SPARKAI_API_SECRET
config = Config()
model = PictureUnderstanding(config)

def gen_audio(chat_query: str, img_path:str):
    '''
    大模型对话
    :param chat_query: 当前用户问题
    :param img_path: 图片的地址
    :return:
    '''

    result = model.understanding(chat_query, img_path)
    return result



# gradio页面绘制
with gr.Blocks() as demo:
    with gr.Column(scale=1):
        img = gr.Image(label='上传图片', type='filepath')
        chat_query = gr.Textbox(label="输入问题", placeholder="输入需要咨询的问题")
        # 按钮
        llm_submit_tab = gr.Button("提交", visible=True)
    with gr.Column(scale=1):
        # 输出组件
        chatbot = gr.Textbox(label="识别结果", lines=10)

    # 按钮出发逻辑
    llm_submit_tab.click(fn=gen_audio, inputs=[chat_query, img], outputs=[chatbot])

if __name__ == "__main__":
    demo.launch()
